package ie.dkit.semanticPhotoWeb.client.panels.photo;

import ie.dkit.semanticPhotoWeb.client.dtos.AlbumDTO;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.MessageBox.ConfirmCallback;
import com.gwtext.client.widgets.MessageBox.PromptCallback;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.HorizontalLayout;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class ImagePanel extends Panel {

	class SummaryChangeAsyncCallback implements AsyncCallback {

		public void onSuccess(Object result) {
			MessageBox.hide();
			photoPanel.m_CurrentImageDTO.imageSummary = photoSummary.getText();
		}

		public void onFailure(Throwable caught) {
			MessageBox.alert("Error", "Unable to change summary");
			photoSummary.setText(photoPanel.m_CurrentImageDTO.imageSummary);
		}

	}

	private Label photoSummary;

	private PhotoPanel photoPanel;

	protected ImagePanel(final PhotoPanel photoPanel) {
		this.photoPanel = photoPanel;
		setAutoScroll(true);
		Panel hp = new Panel();
		hp.setLayout(new HorizontalLayout(2));
		hp.add(new Button("Open Album", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				MessageBox.alert("Open Corresponding Album");
				photoPanel.m_ClientServerConnectorAsync.getAlbum(
						photoPanel.m_CurrentImageDTO,
						new AsyncCallbackAdapter() {
							public void onSuccess(Object result) {
								MessageBox.hide();
								photoPanel.m_CenterPanel
										.addAlbumPanel((AlbumDTO) result);
							}
						});
			}
		}));
		hp
				.add(photoPanel.m_createRelationButton = new Button(
						photoPanel.m_RelationCreator.isRelationMode() ? PhotoPanel.RELATION_MODE_ON
								: PhotoPanel.RELATION_MODE_OFF,
						new ButtonListenerAdapter() {
							public void onClick(Button button, EventObject e) {
								photoPanel.m_RelationCreator
										.setInRelation(photoPanel.m_CurrentImageDTO);
							}
						}));
		hp.add(new Button("Delete Photo", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				MessageBox.confirm("Delete",
						"Are you sure that you want to delete this Photo?",
						new ConfirmCallback() {
							public void execute(String btnID) {
								if (btnID.equals("yes")) {
									photoPanel.m_ClientServerConnectorAsync
											.deleteImage(
													photoPanel.m_CurrentImageDTO,
													new AsyncCallbackAdapter() {
														public void onSuccess(
																Object result) {
															MessageBox
																	.alert("Succesfully deleted");
															photoPanel
																	.destroy();
															photoPanel.m_CenterPanel
																	.remove(photoPanel);
														}
													});
								}
							}
						});
			}
		}));
		add(hp);
		add(new Label(photoPanel.m_CurrentImageDTO.imageTitle));
		add(new Image(photoPanel.m_CurrentImageDTO.thumbnails[2]));
		add(new HTML("<a href=\"" + photoPanel.m_CurrentImageDTO.imageUrl
				+ "\" type=\"image/jpeg\">Download</a>"));
		add(photoSummary = new Label(""
				+ photoPanel.m_CurrentImageDTO.imageSummary));
		if (photoPanel.m_CurrentImageDTO.editable) {
			add(new Button("Set summary", new ButtonListenerAdapter() {
				public void onClick(Button button, EventObject e) {
					MessageBox.prompt("Photo Summary",
							"Please insert a summary for this photo",
							new PromptCallback() {
								public void execute(String btnID, String text) {
									if (btnID.equals("ok")) {
										photoSummary.setText(text);
										photoPanel.m_ClientServerConnectorAsync
												.updateSummary(
														photoPanel.m_CurrentImageDTO,
														text,
														new SummaryChangeAsyncCallback());
									} else {
										MessageBox.hide();
									}
								}
							});
				}
			}));

		}
	}
}
